$(() => {msg.init();});

window.msg = {
    dom: {
        notice: {
            main: null,
            num: null,
            view: null
        },
        task: {
            main: null,
            num: null,
            view: null
        }
    },
    timer: null,
    classTemplate: {
        notice: {
            type: {
                1: {
                    bg: 'bg-info',
                    icon: 'fa-info'
                },
                2: {
                    bg: 'bg-success',
                    icon: 'fa-check'
                },
                3: {
                    bg: 'bg-warning',
                    icon: 'fa-warning'
                },
                4: {
                    bg: 'bg-danger',
                    icon: 'fa-times'
                }
            }
        }
    },
    init: function () {
        this.dom.notice.main = $('#xh_msg_notice');
        this.dom.notice.num  = this.dom.notice.main.find('.xh-msg-noticeNum');
        this.dom.notice.view = $('#xh_msg_notice_view');
        this.dom.task.main   = $('#xh_msg_task');
        this.dom.task.num    = this.dom.task.main.find('.xh-msg-taskNum');
        this.dom.task.view   = $('#xh_msg_task_view');

        //初始化消息
        this.funObj().initMsg();
        this.listener();
    },
    listener: function () {
        //设置消息定时器
        this.timer = setInterval(() => {
            this.funObj().initMsg();
        }, 1000 * 30 * 10);
        //设置消息点击事件
        this.dom.notice.main.on('click', '.xh-msg-notice-list > a', function() {
            msg.funObj().viewNotice($(this).data('id'));
        });
        //消息列表
        this.dom.notice.view.click(() => {
            this.funObj().openNoticePage();
        });
        //设置任务点击事件
        this.dom.task.main.on('click', '.xh-msg-task-list > a', function() {
            msg.funObj().viewTask($(this).data('id'));
        });
        //任务列表
        this.dom.task.view.click(() => {
            this.funObj().openTaskPage();
        });
    },
    funObj: function () {
        return {
            initMsg: () => {
                xhExtra.ajaxRequest('remind/getPreviewMsg', {}, {check: false}, (result) => {
                    this.dom.notice.main.find('.xh-msg-notice-list').remove();
                    this.dom.task.main.find('.xh-msg-task-list').remove();
                    if (xhExtra.hasError(result)) {
                        console.error(result);
                        this.dom.notice.num.text(0);
                        this.dom.task.num.text(0);
                        this.dom.notice.view.parent().before('<li class="xh-msg-notice-list"><a href="#" style="color: red;">获取失败</a></li>');
                        this.dom.task.view.parent().before('<li class="xh-msg-task-list"><a href="#" style="color: red;">获取失败</a></li>');
                        return false;
                    }
                    this.funObj().initNotice(result.notice);
                    this.funObj().initTask(result.task);
                });
            },
            initNotice: (data) => {
                this.dom.notice.num.text(data.total);
                $.each(data.rows, (index, row) => {
                    let readClass = parseInt(row.isRead) ? 'is-read' : '';
                    this.dom.notice.view.parent().before(
                        `<li class="xh-msg-notice-list">
                            <a href="#" data-id="${row.noticeId}">
                                <span class="notification-icon ${this.classTemplate.notice.type[row.type].bg}">
                                    <i class="fa ${this.classTemplate.notice.type[row.type].icon}"></i>
                                </span>
                                <span class="m-left-xs ${readClass}">${row.title}</span>
                                <span class="time text-muted">${row.createAt}</span>
                            </a>
                        </li>`
                    );
                });
            },
            initTask: (data) => {
                this.dom.task.num.text(data.total);
                $.each(data.rows, (index, row) => {
                    // `<li class="xh-msg-list xh-msg-task-list">
                    //     <a class="clearfix" href="#" data-id="${row.taskId}">
                    //         <div class="detail">
                    //             <strong>任务</strong>
                    //             <p class="no-margin">
                    //                 ${row.title}
                    //             </p>
                    //             <small class="text-muted"><i class="fa fa-check text-success"></i>
                    //                 ${row.createAt}
                    //             </small>
                    //         </div>
                    //     </a>
                    // </li>`
                    let readIcon = parseInt(row.isRead) ? '<i class="fa fa-eye text-success"></i>' : '';
                    this.dom.task.view.parent().before(
                        `<li class="xh-msg-task-list">
                            <a class="clearfix" href="#" data-id="${row.taskId}">
                                <div class="detail">
                                    <p class="no-margin">
                                        ${row.title}
                                    </p>
                                    <small class="text-muted">
                                        ${readIcon}
                                        ${row.createAt}
                                    </small>
                                </div>
                            </a>
                        </li>`
                    );
                });
            },
            viewNotice: (id) => {
                xhExtra.ajaxRequest('remind/readNotice/' + id, {}, {check: true}, (info) => {
                    let titleDom = this.dom.notice.main.find(`.xh-msg-notice-list > a[data-id='${id}'] > .m-left-xs`);
                    if (titleDom.length && !titleDom.hasClass('is-read')) {
                        titleDom.addClass('is-read');
                        this.dom.notice.num.text(parseInt(this.dom.notice.num[0].innerText) - 1);
                    }
                    switch (parseInt(info.viewMode)) {
                        case 1:
                            window.top.xhTop.showModal({
                                title: info.title,
                                content: `<p style="margin-top: 10px;margin-right: 10px;word-wrap: break-word;">
                                            ${info.content}
                                         </p>`,
                                button: true,
                            });
                            break;
                        case 2:
                            window.top.swal(info.title, info.content);
                            break;
                        case 3:
                            window.top.xhTop.createNewTab(info.title, info.url);
                            break;
                        case 4:
                            let msgWin = window.open(info.url);
                            msgWin.document.title = info.title;
                            break;
                        default:
                            throw new Error('暂不支持该打开方式');
                    }
                });
            },
            viewTask: (id) => {
                xhExtra.ajaxRequest('remind/readTask/' + id, {}, {check: true}, (info) => {
                    if (parseInt(info.isHandle)) {
                        info.title += '-(已处理)'
                    }
                    switch (parseInt(info.viewMode)) {
                        case 1:
                            window.top.xhTop.showModal({
                                title: info.title,
                                content: `<iframe frameborder="0" src="${info.url}" style="width:100%;height: 500px;"></iframe>`,
                                button: true,
                            });
                            break;
                        case 3:
                            window.top.xhTop.createNewTab(info.title, info.url);
                            break;
                        case 4:
                            let msgWin = window.open(info.url);
                            msgWin.document.title = info.title;
                            break;
                        default:
                            throw new Error('暂不支持该打开方式');
                    }
                });
            },
            openNoticePage: () => {
                window.top.xhTop.createNewTab('消息列表', 'remind/notice');
            },
            openTaskPage: () => {
                window.top.xhTop.createNewTab('任务列表', 'remind/task');
            }
        }
    }
};